
package org.dyndns.cooland.updater.db.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.dyndns.cooland.updater.db.MyBatisConnectionFactory;
import org.dyndns.cooland.updater.db.model.Sites;

/*
 * Class for sites model
 */
public class SitesDAO {
	
	private SqlSessionFactory	sqlSessionFactory;
	
	// Connection connection = null;
	// PreparedStatement ptmt = null;
	// ResultSet resultSet = null;
	
	public SitesDAO () {

		sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
	}
	
	/*
	 * private Connection getConnection() throws SQLException {
	 * 
	 * Connection conn = ConnectionFactory.getInstance().getConnection(); return
	 * conn; }
	 */

	public List<Sites> selectAll() {

		SqlSession session = sqlSessionFactory.openSession();
		try{
			List<Sites> list = session.selectList("Sites.selectAll");
			return list;
		}
		finally{
			session.close();
		}
	}
	
	public Sites selectById(int id) {

		SqlSession session = sqlSessionFactory.openSession();
		try{
			Sites site = session.selectOne("Sites.selectById");
			return site;
		}
		finally{
			session.close();
		}
	}
	
	public void update(Sites site) {

		SqlSession session = sqlSessionFactory.openSession();
		try{
			session.update("Sites.update", site);
			session.commit();
		}
		finally{
			session.close();
		}
	}
	
	public void insert(Sites site) {

		SqlSession session = sqlSessionFactory.openSession();
		try{
			session.insert("Sites.insert", site);
			
			session.commit();
		}
		finally{
			session.close();
		}
	}
	
	public void delete(int id) {

		SqlSession session = sqlSessionFactory.openSession();
		try{
			session.delete("Sites.deleteById", id);
			session.commit();
		}
		finally{
			session.close();
		}
	}
	/*
	 * public int add(Sites bean) {
	 * 
	 * int newId = -1; try{ String sql =
	 * "insert into sites(name,url,description,enabled) values(?,?,?,?)";
	 * connection = getConnection(); ptmt = connection.prepareStatement(sql,
	 * PreparedStatement.RETURN_GENERATED_KEYS); ptmt.setString(1,
	 * bean.getName()); ptmt.setString(2, bean.getUrl()); ptmt.setString(3,
	 * bean.getDescription()); ptmt.setInt(4, bean.getEnabled());
	 * ptmt.executeUpdate(); // get autogenerated key ResultSet keyResultSet =
	 * ptmt.getGeneratedKeys(); if (keyResultSet.next()){ newId = (int)
	 * keyResultSet.getInt(1); } } catch (SQLException e){ e.printStackTrace();
	 * } finally{ try{ if (ptmt != null){ ptmt.close(); } if (connection !=
	 * null){ connection.close(); } } catch (SQLException e){
	 * e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } }
	 * return newId; }
	 * 
	 * public void update(Sites bean) {
	 * 
	 * try{ String sql =
	 * "update sites set name=?,url=?,description=?,enabled=? where id=?";
	 * connection = getConnection(); ptmt = connection.prepareStatement(sql);
	 * ptmt.setString(1, bean.getName()); ptmt.setString(2, bean.getUrl());
	 * ptmt.setString(3, bean.getDescription()); ptmt.setInt(4,
	 * bean.getEnabled()); ptmt.setInt(5, bean.getId()); ptmt.executeUpdate(); }
	 * catch (SQLException e){ e.getSQLState(); e.printStackTrace(); } finally{
	 * try{ if (ptmt != null){ ptmt.close(); } if (connection != null){
	 * connection.close(); } } catch (SQLException e){ e.printStackTrace(); }
	 * catch (Exception e){ e.printStackTrace(); } } }
	 * 
	 * public void delete(Sites bean) {
	 * 
	 * try{ String sql = "delete from sites where id=?"; connection =
	 * getConnection(); ptmt = connection.prepareStatement(sql); ptmt.setInt(1,
	 * bean.getId()); ptmt.executeUpdate(); } catch (SQLException e){
	 * e.printStackTrace(); } finally{ try{ if (ptmt != null){ ptmt.close(); }
	 * if (connection != null){ connection.close(); } } catch (SQLException e){
	 * e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } } }
	 * 
	 * public List<Sites> findAll() {
	 * 
	 * List<Sites> list = new ArrayList<Sites>(); try{ String sql =
	 * "select * from sites"; connection = getConnection(); ptmt =
	 * connection.prepareStatement(sql); resultSet = ptmt.executeQuery(); while
	 * (resultSet.next()){ Sites bean = new Sites();
	 * bean.setId(resultSet.getInt("id"));
	 * bean.setName(resultSet.getString("name"));
	 * bean.setUrl(resultSet.getString("url"));
	 * bean.setDescription(resultSet.getString("description"));
	 * bean.setEnabled(resultSet.getInt("enabled")); list.add(bean); } } catch
	 * (SQLException e){ e.printStackTrace(); } finally{ try{ if (ptmt != null){
	 * ptmt.close(); } if (connection != null){ connection.close(); } } catch
	 * (SQLException e){ e.printStackTrace(); } catch (Exception e){
	 * e.printStackTrace(); } } return list; }
	 */
}
